package com.shujia.flink.tf;

import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo4KeyBy {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(2);

        DataStream<String> linesDS = env.socketTextStream("master", 8888);

        //匿名内部类
        KeyedStream<String, String> keyByDS = linesDS
                .keyBy(new KeySelector<String, String>() {
                    @Override
                    public String getKey(String word) throws Exception {
                        return word;
                    }
                });

        //keyByDS.print();

        //lambda表达式
        linesDS.keyBy(word -> word).print();


        env.execute();
    }
}
